package com.hl.toolkit.server.hao0.wepay.demo.support;

import com.google.common.io.ByteStreams;
import com.me.hao0.common.date.Dates;
import com.me.hao0.wepay.core.Wepay;
import com.me.hao0.wepay.core.WepayBuilder;
import com.me.hao0.wepay.model.pay.JsPayRequest;
import com.me.hao0.wepay.model.pay.JsPayResponse;
import com.me.hao0.wepay.model.pay.QrPayRequest;
import com.me.hao0.wepay.model.refund.RefundApplyRequest;
import com.me.hao0.wepay.model.refund.RefundApplyResponse;
import com.util.LoadProperties;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Map;

/**
 */
// @Component
public class WepayService {

 	private final static String appId= "wx0f25870d81609a51";

 	private final static String appKey= "MAICOOLDIANMIANPAYmaicooldmpay88";

 	private final static String mchId = "1271600201";
 	
 	private final static String subMchId = "1339781201";

 	private final static String payNotifyUrl=LoadProperties.loadPropertiesValue("applicationURL")+"/tycoinPayApp/interface/wepayScann/notiy_return";

	private final static String certFilePath="D:\\wxcert\\cert\\apiclient_cert.p12";


	private Wepay wepay;

	// @PostConstruct
	public WepayService() {
 
		try (InputStream in =new FileInputStream(new File(certFilePath))) {
			// 加载证书文件
			byte[] certs = ByteStreams.toByteArray(in);
			wepay = WepayBuilder.newBuilder(appId, appKey, mchId,subMchId).certPasswd(mchId).certs(certs).build();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public void initWepay(String appId, String appKey, String mchId, byte[] certs) {
   		wepay = WepayBuilder.newBuilder(appId, appKey, mchId,subMchId).certPasswd(mchId).certs(certs).build();

	}

	/**
	 * 动态二维码支付
	 * 
	 * @param orderNumber
	 *            订单号
	 * @return 二维码链接
	 */
	public String qrPay(String orderNumber, String orderName, String clientIp, int orderAmount) {
		QrPayRequest request = new QrPayRequest();
		request.setBody(orderName);
		request.setClientId(clientIp);
		request.setTotalFee(orderAmount);
		if(orderAmount==0){
			request.setTotalFee(1);
		}
		request.setNotifyUrl(payNotifyUrl);
		request.setOutTradeNo(orderNumber);
		request.setTimeStart(Dates.now("yyyyMMddHHmmss"));
		return wepay.pay().qrPay(request);
	}
	
	public JsPayResponse jsPay(String orderNumber, String orderName, String clientIp, int orderAmount,String openId) {
		JsPayRequest request = new JsPayRequest();
		request.setBody(orderName);
		request.setClientId(clientIp);
		request.setTotalFee(orderAmount);
		request.setNotifyUrl(payNotifyUrl);
		request.setOutTradeNo(orderNumber);
		request.setTimeStart(Dates.now("yyyyMMddHHmmss"));
		
     	request.setOpenId(openId);
		
		return wepay.pay().jsPay(request);
	}
	
	/**
	 * 校验签名
	 * 
	 * @param params
	 *            参数(包含sign)
	 * @return 校验成功返回true，反之false
	 */
	public Boolean verifySign(Map<String, ?> params) {
		return wepay.notifies().verifySign(params);
	}

	/**
	 * 通知成功
	 */
	public String notifyOk() {
		return wepay.notifies().ok();
	}

	/**
	 * 通知不成功
	 * 
	 * @param errMsg
	 *            错误消息
	 */
	public String notifyNotOk(String errMsg) {
		return wepay.notifies().notOk(errMsg);
	}

	public RefundApplyResponse refundApply(String orderNumber) {
		RefundApplyRequest req = new RefundApplyRequest();
		req.setOutTradeNo(orderNumber);
		req.setOutRefundNo(orderNumber);
		req.setTotalFee(1);
		req.setRefundFee(1);
		req.setOpUserId(wepay.getMchId());
		return wepay.refund().apply(req);
	}

 

	public Wepay getWepay() {
		return wepay;
	}

	public void setWepay(Wepay wepay) {
		this.wepay = wepay;
	}

}
